package com.cslg.questionnaires.controller;

import com.cslg.questionnaires.entity.Question;
import com.cslg.questionnaires.service.QuestionFormService;
import com.cslg.questionnaires.utils.JsonMsgBuilder;
import com.cslg.questionnaires.utils.TextXssClean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

/**
 * 问卷填写 控制器
 *
 */
@Controller
public class QuestionFormController {
    @Autowired
    private QuestionFormService questionFormService;

    @GetMapping("/QuestionForm/{questionId}")
    public ModelAndView questionFormPage(@PathVariable int questionId){
        HashMap<String, Object> msg = questionFormService.getQuestionForm(questionId);
        int msg_code = (int) msg.get("code");
        if (msg_code == 200){
            return new ModelAndView("QuestionForm",msg);
        }else{
            return new ModelAndView("QuestionFormError",msg);
        }

    }

    @PostMapping("/QuestionForm/submit")
    @ResponseBody
    public HashMap<String,Object> submitForm(@RequestBody HashMap<String,Object> request){
        HashMap<String, Object> msg = questionFormService.submitQuestionForm(request);
        return msg;
    }

    @PostMapping("/QuestionForm/getPerson")
    @ResponseBody
    public HashMap<String,Object> getRecommendPerson(@RequestBody HashMap<String,Object> request){
        String person_name = (String) request.get("name");
        person_name = TextXssClean.cleanXSS(person_name);
        HashMap<String,Object> msg = questionFormService.getPerson(person_name);
        return msg;
    }
}
